package customer.zspt.cn.customer.Widget.chat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.io.File;

import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.bean.ImageUploadResultDao;
import customer.zspt.cn.customer.components.chat.ChatOtherThingContent.ChatImageContent;
import customer.zspt.cn.customer.components.chat.FriendChat;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.utils.NetUtils;
import customer.zspt.cn.customer.utils.SPUtils;
import customer.zspt.cn.customer.utils.TimestampToBj;
import customer.zspt.cn.customer.utils.UnloadFileUtils;
/**
 * Created by Administrator on 2018/7/29 0029.
 */
public class ChatMsgImage extends LinearLayout implements View.OnClickListener{
    private  String token;
    @ViewInject(R.id.header_portrait)
    private  ImageView header_portrait;
    @ViewInject(R.id.msg_img)
    private  ImageView msg_img;
    @ViewInject(R.id.msg_time)
    private  TextView msg_time;
    private BitmapUtils utils;
    private String ImagePath;
    @ViewInject(R.id.loading)
    private LinearLayout loading;
    private UnloadFileUtils unloadFileUtils;
    private FriendChat friendChat;
    @ViewInject(R.id.fail_tip)
    private TextView fail_tip;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.arg1==0){
                loading.setVisibility(GONE);
                msg_img.setOnClickListener(ChatMsgImage.this);
            }else if(msg.arg1==-1){
                fail_tip.setVisibility(VISIBLE);
                loading.setVisibility(GONE);
            }

        }
    };
    public ChatMsgImage(Context context, boolean isLeft) {
        super(context);
        utils = new BitmapUtils(context, constant.IMAGEPATH);
        if(isLeft){
            View.inflate(context, R.layout.chat_msg_ig_left_item,this);
        }else{
            View.inflate(context, R.layout.chat_msg_ig_right_item,this);
        }
        ViewUtils.inject(this);
        msg_img.setOnClickListener(this);
    }
      public ChatMsgImage(Context context, String imagePath, final Activity activity, boolean isLeft){
          super(context);
          View.inflate(context, R.layout.chat_msg_ig_right_item,this);
          ViewUtils.inject(this);
          this.friendChat = (FriendChat) activity;
          utils = new BitmapUtils(context, constant.IMAGEPATH);
          utils.configDefaultLoadingImage(R.drawable.loadimg);
          ImagePath = imagePath;
          unloadFileUtils = new UnloadFileUtils();
          loading.setVisibility(VISIBLE);
          Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
          msg_img.setImageBitmap(bitmap);
           token = SPUtils.getString(context,"token","");
          if(NetUtils.getNetWorkState(context)==NetUtils.NOTNET){
              fail_tip.setVisibility(VISIBLE);
              loading.setVisibility(GONE);
          }else{
              unloadFileUtils.onLoadFile(imagePath,"image/jpg", constant.uploadImage,token);
          }
          /**获取到信息后的操作*/
          unloadFileUtils.getResultLintener(new UnloadFileUtils.OnResultLintener() {
              @Override
              public void OnSuccess(String result) {
                  Message message = new Message();
                  message.arg1=0;
                  mHandler.sendMessage(message);
                  FriendChat friendChat = (FriendChat) activity;
                  friendChat.pushFileData(result,"img",0L);
                  setImageUrl(result);
              }
              @Override
              public void OnError() {

              }
              @Override
              public void OnFail() {
                  Message message = new Message();
                  message.arg1=-1;
                  mHandler.sendMessage(message);
              }
          });
          loading.setOnClickListener(this);
          fail_tip.setOnClickListener(this);
      }
    public void setImageUrl(String result){
          Gson gson = new Gson();
          ImageUploadResultDao imageUploadResultDao = gson.fromJson(result, ImageUploadResultDao.class);
          ImageUploadResultDao.ImageUploadResult data = imageUploadResultDao.data;
        if(imageUploadResultDao.code==0){
            ImagePath = data.url;
            fail_tip.setVisibility(GONE);
        }else{
            fail_tip.setVisibility(VISIBLE);
        }
      }
      public void setHeader_portrait(String url)
      {
             utils.display(header_portrait,url);
      }
      public void setMsg_img(String url)
      {
          File imgFile = utils.getBitmapFileFromDiskCache(url);
          ImagePath = url;
          if(imgFile==null)
          {
              utils.display(msg_img,url);
          }
          else{
              utils.display(msg_img,imgFile.getPath());
          }
      }
    public void setTime(long time,long ChatTime)
    {
        if (time - ChatTime > 3 * 60 * 1000) {
            msg_time.setText(TimestampToBj.getBeijingTime(time));
        } else {
            msg_time.setVisibility(View.GONE);
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.loading:
                break;
            case R.id.msg_img:
                 Intent intent = new Intent(getContext(), ChatImageContent.class);
                    intent.putExtra("imgUrl",ImagePath);
                    getContext().startActivity(intent);
                break;
            case R.id.fail_tip:
                fail_tip.setVisibility(GONE);
                loading.setVisibility(VISIBLE);
                unloadFileUtils.onLoadFile(ImagePath,"image/jpg", constant.uploadImage,token);
                break;
        }

    }
}
